package com.controller;

import com.alibaba.fastjson.JSON;
import com.pojo.User;
import com.service.UserService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Controller
public class RedisController {
    @Resource
    private UserService userService;

    @Resource
    private RedisTemplate<String, Object> redisTemplate;//<String, Object>

    @PostMapping("/redisSet")
    public void redisSet(@RequestParam("key") String key,@RequestParam("value") String value) {
        redisTemplate.opsForValue().set(key, value);
        String s = (String)redisTemplate.opsForValue().get(key);
        System.out.println(s);
    }

    @PostMapping("/redisList")
    @ResponseBody
    public List<String> listTest(){
        this.redisTemplate.delete("names");
        this.redisTemplate.opsForList().rightPushAll("names","jack","joe","rega","tom","ted");
        List<String> names = (List)this.redisTemplate.opsForList().range("names", 0, -1);
        for (String name : names) {
            System.out.println(name);
        }
        return names;
    }

    @PostMapping("/mapTest")
    public Map<Object, Object> mapTest(){
        this.redisTemplate.delete("userMap");
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("name","jack");
        map.put("age",18);
        map.put("password","123456");
        this.redisTemplate.opsForHash().putAll("userMap", map);
        Map<String,Object> userMap = (Map)this.redisTemplate.opsForHash().entries("userMap");
        Map<Object,Object> userMap1 = (Map)this.redisTemplate.opsForHash().entries("userMap");
        return userMap1;
    }


    @PostMapping("/redisLogin")
    @ResponseBody
    public String redisLogin(){
        Boolean isUser = this.redisTemplate.hasKey("user");
        User user = null;
        if(isUser){
            user = (User)this.redisTemplate.opsForValue().get("user");

            System.out.println("从redis缓存中获取数据!");
        }else {
            user = userService.getById(1);
            System.out.println("从mysql数据库中获取数据!");
            this.redisTemplate.opsForValue().set("user",user,10,TimeUnit.SECONDS);
        }

        return user.getName() + "登录成功!";
    }

    @RequestMapping("/redisGetUserList")
    @ResponseBody
    public List<User> redisGetUserList(){
        Boolean isUser = this.redisTemplate.hasKey("list");
        List<User> list = null;
        if(isUser){
            list = (List)this.redisTemplate.opsForValue().get("list");

            System.out.println("从redis缓存中获取数据!");
        }else {
            list = userService.list();
            System.out.println("从mysql数据库中获取数据!");
            this.redisTemplate.opsForValue().set("list",list,10,TimeUnit.SECONDS);
        }
        return list;
    }


    @PostMapping("/getRedis")
    @ResponseBody
    public User getRedis(){
        User user  = new User();
        user.setId(11111);
        user.setName("sdddf");

        redisTemplate.opsForValue().set("user", user);
        String string = redisTemplate.opsForValue().get("user").toString();
        User user1 = JSON.parseObject(string, User.class);
        return user1;
    }




}
